package com.kandroid.designpattern.state.demo2;

import java.util.List;

public abstract class BaseStateClass implements ATMState {
    public static final String ACTION_INSERT_BANKCARD = "insertBankCard";   //插入银行卡
    public static final String ACTION_BACK_BANKCARD = "backBankCard";       //退出银行卡
    public static final String ACTION_INPUT_PASSWORD = "inputPassword";     //输入密码
    public static final String ACTION_INPUT_MONEY = "inputMoney";           //输入取款金额
    public static final String ACTION_TAP_OK_BUTTON = "tapOkButton";        //点击确定按钮


    protected ATMAction atmAction;
    private List<String> supportedActions = getSupportedActions();        //状态支持的动作

    public BaseStateClass(ATMAction atmAction) {
        this.atmAction = atmAction;
    }

    protected abstract List<String> getSupportedActions();

    @Override
    public void insertBankCard() {
        if (supportedActions.contains(ACTION_INSERT_BANKCARD)) {
            //新增: 判断ATM是否有现金
            if(atmAction.hasEnoughATMMoney()) {
                atmAction.changeState(ATMState.STATE_HAS_BANKCARD);
            } else {
                atmAction.changeState(ATMState.STATE_BANK_NO_MONEY);
            }
        } else {
            print("【错误】已插入银行卡！");
        }
    }

    @Override
    public void backBankCard() {
        if (supportedActions.contains(ACTION_BACK_BANKCARD)) {
            atmAction.changeState(ATMState.STATE_NO_BANKCARD);
        } else {
            print("【错误】无法退卡！");
        }
    }

    @Override
    public void inputPassword() {
        if (supportedActions.contains(ACTION_INPUT_PASSWORD)) {
            atmAction.changeState(ATMState.STATE_DECODING);
        } else {
            print("【错误】无法输入密码！");
        }
    }

    @Override
    public void inputMoney(int money) {
        if (supportedActions.contains(ACTION_INPUT_MONEY)) {
            atmAction.changeState(ATMState.STATE_FETCH_MONEY);
        } else {
            print("【错误】当前无法输入取款金额！");
        }
    }

    @Override
    public void tapOkButton() {
        if (supportedActions.contains(ACTION_TAP_OK_BUTTON)) {
            if (!atmAction.hasEnoughCardMoney()) {
                atmAction.changeState(ATMState.STATE_INSUFFICIENT_BALANCE);
            } else {
                atmAction.withdrawMoney();
                atmAction.changeState(ATMState.STATE_OUT_MONEY);
            }
        } else {
            print("【错误】当前无法进行取款操作！");
        }
    }

    protected void print(String msg) {
        System.out.println(msg);
    }
}
